using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

using SSOConfig;
using SSOConfigTool;
using SSOConfigTool.Windows.Enums;

namespace SSOConfigTool.Windows
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            this.sSOApplicationBindingSource.AddNew();
            this.ApplicationExists = false;
            this.CurrentApplication = null;
        }

        #region Private instance fields

        private bool _appExists = false;
        private SSOApplication _currentApp;
        private List<Field> _fields;
        string _xmlFilePath;

        #endregion

        #region Private properties

        private bool ApplicationExists
        {
            get { return this._appExists; }
            set
            {
                this._appExists = value;
                //enable menu items:
                this.deleteToolStripMenuItem.Enabled = value;
                //prevent name change for existing app:
                this.txtName.Enabled = !value;
            }
        }

        private SSOApplication CurrentApplication
        {
            get { return this._currentApp; }
            set
            {
                this._currentApp = value;
                //refresh the binding sources:
                this._fields = null;
                this.sSOApplicationBindingSource[0] = value;
                this.fieldsBindingSource.DataSource = this.Fields;
                //enable input:
                this.grpApp.Enabled = this.grpFields.Enabled = (value != null);
                //enable menu items:
                this.saveToXMLToolStripMenuItem.Enabled = this.codeGenToolStripMenuItem.Enabled =
                    this.saveToolStripMenuItem.Enabled  = (value != null);
            }
        }

        private List<Field> Fields
        {
            get
            {
                if (this._fields == null)
                {
                    this._fields = new List<Field>();
                    if (this._currentApp != null && this._currentApp.Fields != null)
                    {
                        this._fields.AddRange(this._currentApp.Fields);
                    }
                }
                return this._fields;
            }
        }

        #endregion

        #region Event handlers

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmOpenApplication open = new frmOpenApplication();
            open.ShowDialog();
            if (!string.IsNullOrEmpty(open.SelectedApplication))
            {
                this.CurrentApplication = SSOApplication.LoadFromSSO(open.SelectedApplication);
            }
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SSOApplication newApp = new SSOApplication();
            //set defaults:
            newApp.AdminGroup = Default.AdminGroup;
            newApp.UserGroup = Default.UserGroup;
            this._fields = new List<Field>();
            newApp.Fields = this._fields.ToArray();
            this.CurrentApplication = newApp;
            this.txtName.Enabled = true;
        }


        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.IsApplicationValid())
            {
                this.CurrentApplication.SaveToSSO();
                this.ApplicationExists = true;
            }
        }

        private bool IsApplicationValid()
        {
            bool valid = true;
            this.fieldsBindingSource.EndEdit();
            this.CurrentApplication.Fields = this._fields.ToArray();
            try
            {
                this.CurrentApplication.Validate();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Application Not Valid", MessageBoxButtons.OK, MessageBoxIcon.Error);
                valid = false;
            }
            return valid;
        }


        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.SSOHelper.DeleteApplication(this.CurrentApplication.Name);
        }

        #endregion

        private void saveToXMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._xmlFilePath = null;
            frmXMLFilename xml = new frmXMLFilename();
            xml.Mode = XmlMode.Save;
            xml.ShowDialog();
            if (!string.IsNullOrEmpty(xml.XMLFilePath))
            {
                this._xmlFilePath = xml.XMLFilePath;
                if (this.IsApplicationValid())
                {
                    this.CurrentApplication.SaveToXml(this._xmlFilePath);
                }
            }
        }

        private void importFromXMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._xmlFilePath = null;
            frmXMLFilename xml = new frmXMLFilename();
            xml.Mode = XmlMode.Load;
            xml.ShowDialog();
            if (!string.IsNullOrEmpty(xml.XMLFilePath))
            {
                this._xmlFilePath = xml.XMLFilePath;
                this.CurrentApplication = SSOApplication.LoadFromXml(this._xmlFilePath);
            }
        }

        private void generateConfigClassToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmGetDetails cls = new frmGetDetails();
            cls.Mode = GenerateMode.Class;
            cls.ShowDialog();
            if (!string.IsNullOrEmpty(cls.FilePath))
            {
                ConfigTool.GenerateClass(this.CurrentApplication, cls.Namespace, cls.FilePath);
            }
        }

        private struct Default
        {
            public const string UserGroup = "BizTalk Application Users";
            public const string AdminGroup = "BizTalk Server Administrators";
        }

        private void generateConfigAssemblyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmGetDetails cls = new frmGetDetails();
            cls.Mode = GenerateMode.Assembly;
            cls.ShowDialog();
            if (!string.IsNullOrEmpty(cls.FilePath))
            {
                ConfigTool.GenerateAssembly(this.CurrentApplication, cls.Namespace, cls.FilePath);
            }
        }
    }
}